package com.ruoyi.web.controller.banner;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.banner.service.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.io.PrintStream;
import java.util.List;
import com.ruoyi.banner.domain.Banner;


@RestController
@RequestMapping("/banner/banner")
public class BannerController extends BaseController {
    @Autowired
    private IBannerService bannerService;
    /**
     * 查询banner列表
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(Banner banner)
    {
        startPage();
        List<Banner> list = bannerService.selectBannerList(banner);
        return getDataTable(list);
    }


    /**
     * 导出banner列表
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:export')")
    @Log(title = "banner", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Banner banner)
    {
        List<Banner> list = bannerService.selectBannerList(banner);
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        return util.exportExcel(list, "banner数据");
    }

    /**
     * 获取banner详细信息
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:query')")
    @GetMapping(value = "/query/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bannerService.selectBannerById(id));
    }

    /**
     * 新增banner
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:add')")
    @Log(title = "banner", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Banner banner)
    {
        return toAjax(bannerService.insertBanner(banner));
    }

    /**
     * 修改banner
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:edit')")
    @Log(title = "banner", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Banner banner)
    {
        banner.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(bannerService.updateBanner(banner));
        //test

    }

    /**
     * 删除banner
     */
    @PreAuthorize("@ss.hasPermi('banner:banner:remove')")
    @Log(title = "banner", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bannerService.logicDeleteBanner(ids,DicConstants.BUSINESS_Manager.getCode(), SecurityUtils.getUserId()));
    }
}
